﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports System.Linq
Imports ISNet.WebUI.WebTreeView
Imports log4net


Partial Class mtActivityType
    Inherits System.Web.UI.Page

    Private Shared logger As ILog = LogManager.GetLogger("mtActivityType")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >> ครุภัณฑ์"
        'Session("users") = "bb0011"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session.Remove("SearchTxt")
            Session("SearchTxt") += " and activityTypes.PlanYear = " & PlanYearsTxt.Text
        Else
            'PlanYearsTxt.Text = Session("PlanYearTxt")
        End If
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New MasterDataSetTableAdapters.ActivityTypesTableAdapter
        Dim dtTable As New MasterDataSet.ActivityTypesDataTable

        Dim sql As String = "select *, "
        sql += " CASE "
        sql += " WHEN TemplateName IS NULL THEN 0 "
        sql += " WHEN TemplateName = '' THEN 0 "
        sql += " ELSE 1 "
        sql += " END Attachments_amount "
        'sql += " ,'' AS BudgetDetails_id "
        sql += " from activityTypes "
        'sql += " LEFT JOIN BudgetDetails ON ActivityTypes.CmmtItem_CmmtCode = BudgetDetails.Cmmt_CmmtCode "
        'sql += " and ActivityTypes.PlanYear = BudgetDetails.Cmmt_PlanYear "
        sql += " WHERE 1 = 1 "

        'Dim sql As String = "select ActivityTypes.ActivityTypeCode, ActivityTypes.PlanYear, ActivityTypes.Description, "
        'sql += " ActivityTypes.Price, ActivityTypes.AssetGroup, ActivityTypes.AttachmentFlag, "
        'sql += " ActivityTypes.CreatedDate, ActivityTypes.UpdatedDate, ActivityTypes.CreatedBy, ActivityTypes.UpdatedBy,"
        'sql += " ActivityTypes.Id, ActivityTypes.CmmtItem_CmmtCode, ActivityTypes.FileNamePattern, "
        'sql += " ActivityTypes.TemplateName, "
        'sql += " CASE "
        'sql += " WHEN TemplateName IS NULL THEN 0 "
        'sql += " WHEN TemplateName = '' THEN 0 "
        'sql += " ELSE 1 "
        'sql += " END Attachments_amount "
        ''sql += " ,'' AS BudgetDetails_id "
        'sql += " from activityTypes "
        ''sql += " LEFT JOIN BudgetDetails ON ActivityTypes.CmmtItem_CmmtCode = BudgetDetails.Cmmt_CmmtCode "
        ''sql += " and ActivityTypes.PlanYear = BudgetDetails.Cmmt_PlanYear "
        'sql += " WHERE 1 = 1 "
        ' sql += " and PlanYear like '" & PlanYearsTxt.Text & "'"

        sql += Session("SearchTxt")

        sql += " ORDER BY activityTypecode ,activityTypes.PlanYear desc, AssetGroup "

        adapter.SearchActType(dtTable, sql)
        'Response.Write(sql)

        Session("count_data") = dtTable.Rows.Count

        dtTable.Columns.Add("AttFlag", Type.GetType("System.Boolean"))

        Dim atf As String = ""
        For i = 0 To dtTable.Count - 1
            atf = dtTable.Rows(i).Item("AttachmentFlag")
            If atf = "Y" Then
                dtTable.Rows(i).Item("AttFlag") = True
            Else
                dtTable.Rows(i).Item("AttFlag") = False
            End If
        Next

        e.DataSource = dtTable
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(2).ForceNoEdit = True
        e.Row.Cells(8).ForceNoEdit = True
        e.Row.Cells(9).ForceNoEdit = True
        e.Row.Cells(10).ForceNoEdit = True
        'If e.Row.Type = RowType.Record Then
        '    Dim dr As DataRowView = DirectCast(e.Row.DataRow, DataRowView)
        '    Dim Attachment As String = dr("AttachmentFlag").ToString()

        '    If Attachment = "Y" Then
        '        e.Row.Cells(7).Value = True
        '    Else
        '        e.Row.Cells(7).Value = False
        '    End If
        'End If
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New MasterDataSetTableAdapters.ActivityTypesTableAdapter

        Dim check = adapter.CheckExistActType(e.Row.Cells(1).Value, e.Row.Cells(2).Value)

        If Val(check) > 0 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลประเภทกิจกรรมซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            Dim id As Decimal
            Dim container As ISNet.WebUI.WebGrid.WebGridCellTemplateContainer
            container = e.Row.Cells(7).GetTemplateContainer()

            Dim Attachment As String
            Dim dr As DataRow = e.Row.DataRow
            If e.Row.Cells(7).Value = True Then
                Attachment = "Y"
                dr("AttachmentFlag") = "Y"
                dr("AttFlag") = True
            Else
                Attachment = "N"
                dr("AttachmentFlag") = "N"
                dr("AttFlag") = False
            End If
            id = adapter.InsertActiType(e.Row.Cells(1).Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, e.Row.Cells(4).Value, e.Row.Cells(5).Value, Attachment, Date.Now, Session("users"), If(e.Row.Cells(6).Value = "", Nothing, e.Row.Cells(6).Value))

            dr("id") = id

            logger.Info("User " & Session("users") & " insert activity type " & e.Row.Cells(1).Value)
        End If

        ' WebGrid1.ClientAction.Refresh()

    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim id As String
        id = e.Row.KeyValue
        'WebGrid1.ClientAction.Alert(iyear)

        Dim adapter As New MasterDataSetTableAdapters.ActivityTypesTableAdapter
        Dim check = adapter.CheckExistUpdate(e.Row.Cells(1).Value, e.Row.Cells(2).Value, id)

        If Val(check) > 1 Then
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลประเภทกิจกรรมซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            'Dim container As ISNet.WebUI.WebGrid.WebGridCellTemplateContainer
            'container = e.Row.Cells(5).GetTemplateContainer()

            Dim Attachment As String
            Dim dr As DataRowView = DirectCast(e.Row.DataRow, DataRowView)
            If e.Row.Cells(7).Value.ToString = True Then
                Attachment = "Y"
                dr("AttachmentFlag") = "Y"
                dr("AttFlag") = True
            Else
                Attachment = "N"
                dr("AttachmentFlag") = "N"
                dr("AttFlag") = False
            End If

            Dim adap2 As New MasterDataSet2TableAdapters.checkuseTableAdapter
            Dim dt As MasterDataSet.ActivityTypesDataTable
            dt = adapter.GetDataById(Val(id))
            Dim oldcode As String = dt.Item(0).ActivityTypeCode
            Dim oldyear As Integer = dt.Item(0).PlanYear
            Dim used As Integer = adap2.CheckActType(oldcode, oldyear)
            If oldcode.Trim = e.Row.Cells(1).Value.ToString.Trim And oldyear = Val(e.Row.Cells(2).Value) Then
                used = 0
            End If
            If used = 0 Then
                adapter.UpdateActivType(e.Row.Cells(1).Value, e.Row.Cells(2).Value, e.Row.Cells(3).Value, Val(e.Row.Cells(4).Value), e.Row.Cells(5).Value, Attachment, Date.Now, Session("users"), If(e.Row.Cells(6).Value = "", Nothing, e.Row.Cells(6).Value), id)
                logger.Info("User " & Session("users") & " insert activity type " & e.Row.Cells(1).Value)
            Else
                WebGrid1.ClientAction.Refresh()
                WebGrid1.ClientAction.Alert("ไม่สามารแกไขข้อมูลได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
            End If
        End If
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim code As String = TryCast(Request("code"), String)
            Dim yr As String = TryCast(Request("year"), String)
            Dim adapter As New MasterDataSetTableAdapters.ActivityTypesTableAdapter
            Dim adap2 As New MasterDataSetTableAdapters.checkusedTableAdapter
            ' MsgBox(Deletet)
            Try
                Dim used As Integer = adap2.CheckActivityType(code, Val(yr))
                If type = "1" Then
                    If used = 0 Then
                        Dim adapter2 As New MasterDataSetTableAdapters.ActivityTypesFundcenterTableAdapter
                        adapter2.DeleteFromActivityType(Deletet)
                        adapter.DeleteActivType(Deletet)
                    Else
                        WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
                    End If
                ElseIf type = "2" Then

                    If used = 0 Then
                        For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                            Dim adapter2 As New MasterDataSetTableAdapters.ActivityTypesFundcenterTableAdapter
                            adapter2.DeleteFromActivityType(keyValue)
                            adapter.DeleteActivType(keyValue)
                        Next keyValue

                    Else
                        WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลบางรายการได้เนื่องจากข้อมูลถูกนำไปใช้งานแล้ว")
                    End If
                End If

            Catch ex As Exception
                WebGrid1.ClientAction.Alert("ไม่สามารลบข้อมูลได้กรุณาติดต่อผู้ดูแลระบบ")
            End Try
            WebGrid1.ClientAction.Refresh()
        End If

    End Sub
    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        Session("SearchTxt") = ""

        If PlanYearsTxt.Text <> "" Then
            Session("SearchTxt") += " and activityTypes.PlanYear = '" & PlanYearsTxt.Text & "'"
        End If
        If CodeTxt.Text.Trim <> "" Then
            Session("SearchTxt") += " and ActivityTypeCode like '%" & CodeTxt.Text.Trim & "%'"
        End If
        If SearchTxt.Text.Trim <> "" Then
            Session("SearchTxt") += " and Description like '%" & SearchTxt.Text.Trim & "%'"
        End If
        If Val(AssetGroupCombo.Value) <> 0 Then
            Session("SearchTxt") += " and AssetGroup = '" & AssetGroupCombo.Value & "'"
        End If
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub

    'Protected Sub AssetGroupCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles AssetGroupCombo.InitializeDataSource
    '    Dim dt As New DataTable
    '    dt.Columns.Add("AssetGroup")
    '    dt.Columns.Add("Description")

    '    dt.Rows.Add(1, "ครุภัณฑ์สำนักงาน")
    '    dt.Rows.Add(2, "ครุภัณฑ์คอมพิวเตอร์")
    '    dt.Rows.Add(3, "ครุภัณฑ์ขนส่ง")

    '    e.DataSource = dt
    'End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

    Protected Sub CommtItemCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)

        Dim adap As New MasterDataSetTableAdapters.CmmtsTableAdapter
        Dim dt As New MasterDataSet.CmmtsDataTable
        dt = adap.GetDataBy18(Val(Session("PlanYearTxt")))
        e.DataSource = dt

    End Sub
End Class
